package logic

import (
	"base/models/mysql"
	"base/pkg"
	"context"
	"errors"

	"base/baserpc/baserpc"
	"base/baserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RealLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRealLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RealLogic {
	return &RealLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 实名认证
func (l *RealLogic) Real(in *baserpc.RealRequest) (*baserpc.RealResponse, error) {
	// todo: add your logic here and delete this line
	var real mysql.Real
	real.UserId = int(in.Userid)
	if err := real.GetName(); err != nil {
		logx.Error("用户查询不到")
		return nil, err
	}
	if real.ID > 0 {
		logx.Error("用户已注册")
		return nil, errors.New("用户已注册")
	}
	if !pkg.RealName(in.Idnumber, in.Name) {
		logx.Error("用户第三方接口返回失败")
		return nil, errors.New("第三方接口返回失败")
	}

	real.Name = in.Name
	if err := real.AddUser(); err != nil {
		logx.Error("用户已注册")
		return nil, err
	}
	return &baserpc.RealResponse{
		Id: int64(real.ID),
	}, nil
}
